"""
API请求模型定义
"""
from pydantic import BaseModel, Field, validator
from typing import List, Optional

from fin_senti_entity_platform.utils.constants import REGEX_PATTERNS


class Entity(BaseModel):
    """实体信息模型"""
    start: int = Field(..., ge=0, description="实体在文本中的起始位置")
    end: int = Field(..., ge=0, description="实体在文本中的结束位置")
    type: str = Field(..., description="实体类型")
    text: Optional[str] = Field(None, description="实体文本内容")
    sentiment: Optional[str] = Field(None, description="实体情感标签")
    
    @validator('end')
    def end_must_be_greater_than_start(cls, v, values):
        if 'start' in values and v <= values['start']:
            raise ValueError("结束位置必须大于起始位置")
        return v


class SentimentRequest(BaseModel):
    """情感分析请求模型"""
    text: str = Field(..., min_length=1, max_length=10000, description="输入文本")
    entities: List[Entity] = Field(..., min_items=1, description="实体列表")
    
    @validator('text')
    def text_length_validation(cls, v):
        if len(v) > 10000:
            raise ValueError("文本长度超过限制")
        return v


class EntityRequest(BaseModel):
    """实体识别请求模型"""
    text: str = Field(..., min_length=1, max_length=10000, description="输入文本")
    entity_types: Optional[List[str]] = Field(None, description="要识别的实体类型")
    
    @validator('text')
    def text_length_validation(cls, v):
        if len(v) > 10000:
            raise ValueError("文本长度超过限制")
        return v


class BatchSentimentRequest(BaseModel):
    """批量情感分析请求模型"""
    texts: List[str] = Field(..., min_items=1, max_items=100, description="输入文本列表")
    entities_list: List[List[Entity]] = Field(..., description="实体列表的列表")
    
    @validator('entities_list')
    def entities_list_length_must_match_texts(cls, v, values):
        if 'texts' in values and len(v) != len(values['texts']):
            raise ValueError("实体列表的数量必须与文本数量一致")
        return v
    
    @validator('texts')
    def texts_length_validation(cls, v):
        for text in v:
            if len(text) > 10000:
                raise ValueError("文本长度超过限制")
        return v